package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <h3>leyou-project</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-05-28 12:08
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController{


    private SpuService spuService;
    private SkuService skuService;
    private SpuDetailService spuDetailService;

    //构造方法注入
    public GoodsController(SpuService spuService, SkuService skuService, SpuDetailService spuDetailService) {
        this.spuService = spuService;
        this.skuService = skuService;
        this.spuDetailService = spuDetailService;
    }

    /**
     *@描述  根据条件分页查询spu表,封装在page
     *@参数  [brandId, categoryId, id, page, rows, saleable]
     *@返回值  org.springframework.http.ResponseEntity<com.leyou.common.dto.PageDTO<com.leyou.item.dto.SpuDTO>>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> spuPageQuery(@RequestParam(value = "brandId", required = false)Long brandId,
                                                        @RequestParam(value = "categoryId", required = false)Long categoryId,
                                                        @RequestParam(value = "id", required = false)Long id,
                                                        @RequestParam(value = "page", defaultValue = "1")Long page,
                                                        @RequestParam(value = "rows", defaultValue = "5")Long rows,
                                                        @RequestParam(value = "saleable", required = false)Boolean saleable){

        PageDTO<SpuDTO> pageDTO = spuService.queryPage(brandId, categoryId, id, page, rows, saleable);
        return ResponseEntity.ok(pageDTO);
    }

    /**
     *@描述    新增商品
     *@参数  [spuDTO]
     *@返回值  org.springframework.http.ResponseEntity<java.lang.Void>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.noContent().build();
    }

    /**
     *@描述  修改商品的上下架信息
     *@参数  [id, saleable]
     *@返回值  org.springframework.http.ResponseEntity<java.lang.Void>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id")Long id,
                                               @RequestParam("saleable")Boolean saleable) {
        spuService.updateSaleable(id, saleable);
        return ResponseEntity.noContent().build();
    }

    /**
     *@描述      根据ID查询商品
     *@参数  [id]
     *@返回值  org.springframework.http.ResponseEntity<com.leyou.item.dto.SpuDTO>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id) {
        Spu spu = spuService.getById(id);
        return ResponseEntity.ok(new SpuDTO(spu));
    }

    /**
     *@描述   根据id查询商品信息,sku, spuDetail信息
     *@参数  [id]
     *@返回值  org.springframework.http.ResponseEntity<com.leyou.item.dto.SpuDTO>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuDetailAndSkuById(@PathVariable("id")Long id) {
        SpuDTO spuDTO = spuService.querySpuDetailAndSkuById(id);
        return ResponseEntity.ok(spuDTO);
    }
    /**
     *@描述   更新商品
     *@参数  [spuDTO]
     *@返回值  org.springframework.http.ResponseEntity<com.leyou.item.dto.SpuDTO>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.noContent().build();
    }

    /**
     *@描述   根据skuid的集合查询sku集合
     *@参数  [ids]
     *@返回值  org.springframework.http.ResponseEntity<java.util.List<com.leyou.item.dto.SkuDTO>>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuById(@RequestParam("ids") List<Long> ids){
        List<Sku> skus = skuService.listByIds(ids);
        return ResponseEntity.ok(SkuDTO.convertEntityList(skus));
    }

    /**
     *@描述   根据spuid获取sku集合
     *@参数  [id]
     *@返回值  org.springframework.http.ResponseEntity<java.util.List<com.leyou.item.dto.SkuDTO>>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long id) {
        List<Sku> skuList = skuService.lambdaQuery().eq(Sku::getSpuId, id).list();
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuList));
    }

    /**
     *@描述   根据spuid查询spudetail
     *@参数  [id]
     *@返回值  org.springframework.http.ResponseEntity<com.leyou.item.dto.SpuDetailDTO>
     *@创建人  hhw
     *@创建时间  2020/5/28
     *@修改人和其它信息
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long id) {
        SpuDetail spuDetail = spuDetailService.getById(id);
        return ResponseEntity.ok(new SpuDetailDTO(spuDetail));
    }
    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id spu的id
     * @param searching 是否参与搜索
     * @return 规格参数值
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false) Boolean searching){
        return ResponseEntity.ok().body(spuDetailService.querySpecValues(id, searching));
    }

//    @PutMapping("/stock/minus")
//    public ResponseEntity<Void> updateSoldBySku(@RequestParam)

    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }
}
